package com.quxiu.system.controller;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.quxiu.common.core.exception.ServiceException;
import com.quxiu.common.core.utils.StringUtils;
import com.quxiu.common.security.utils.SecurityUtils;
import com.quxiu.system.domain.vo.BpmProcVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.quxiu.common.log.annotation.Log;
import com.quxiu.common.log.enums.BusinessType;
import com.quxiu.common.security.annotation.RequiresPermissions;
import com.quxiu.system.domain.BpmProc;
import com.quxiu.system.service.IBpmProcService;
import com.quxiu.common.core.web.controller.BaseController;
import com.quxiu.common.core.web.domain.AjaxResult;
import com.quxiu.common.core.utils.poi.ExcelUtil;
import com.quxiu.common.core.web.page.TableDataInfo;

/**
 * 流程定义Controller
 *
 * @author quxiu
 * @date 2024-06-14
 */
@RestController
@RequestMapping("/bpm/proc")
public class BpmProcController extends BaseController {
    @Resource
    private IBpmProcService bpmProcService;

    /**
     * 查询流程定义列表
     */
    @RequiresPermissions("bpm:proc:list")
    @GetMapping("/list")
    public TableDataInfo list(BpmProc bpmProc) {
        startPage();
        List<BpmProc> list = bpmProcService.selectBpmProcList(bpmProc);
        return getDataTable(list);
    }

    /**
     * 获取流程定义详细信息
     */
    @GetMapping(value = "/{procId}")
    public AjaxResult getInfo(@PathVariable("procId") Long procId) {
        return success(bpmProcService.selectBpmProcByProcId(procId));
    }

    /**
     * 获取流程定义详细信息
     */
    @PostMapping(value = "/getProcByVersionId")
    public AjaxResult getInfoByVersionId(@RequestParam("versionId") Long versionId) {
        return success(bpmProcService.selectBpmProcByVersionId(versionId));
    }

    /**
     * 新增流程定义
     */
    @RequiresPermissions("bpm:proc:add")
    @Log(title = "流程定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BpmProcVo params) {
        this.verify(params);
        Long userId = SecurityUtils.getUserId();
        params.setOptUserId(userId);
        bpmProcService.insertBpmProc(params);
        return success();
    }

    /**
     * 修改流程定义
     */
    @RequiresPermissions("bpm:proc:edit")
    @Log(title = "流程定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BpmProcVo params) {
        if (params.getProcId() == null) {
            throw new ServiceException("流程ID不能为空");
        }
        this.verify(params);
        Long userId = SecurityUtils.getUserId();
        params.setOptUserId(userId);
        bpmProcService.updateBpmProc(params);
        return success();
    }

    /**
     * 校验流程定义参数
     * @param params
     */
    private void verify(BpmProcVo params) {
        if (StringUtils.isBlank(params.getProcName())) {
            throw new ServiceException("流程名称不能为空");
        }

        if (params.getFormConfig() == null) {
            throw new ServiceException("表单配置不能为空");
        }

        if (params.getNodeConfig() == null) {
            throw new ServiceException("节点配置不能为空");
        }
    }

    /**
     * 删除流程定义
     */
    @RequiresPermissions("bpm:proc:remove")
    @Log(title = "流程定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{procId}")
    public AjaxResult remove(@PathVariable Long procId) {
        return toAjax(bpmProcService.deleteBpmProcByProcId(procId));
    }

    /**
     * 按分组获取流程定义
     */
    @PostMapping("/getProcForGroup")
    public AjaxResult getProcForGroup() {
        Map<String, List<BpmProc>> map = bpmProcService.getProcForGroup();
        return success(map);
    }

}
